
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>安生</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description">
<meta property="og:type" content="website">
<meta property="og:title" content="安生">
<meta property="og:url" content="http://localhost/index.html">
<meta property="og:site_name" content="安生">
<meta property="og:description">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="安生">
<meta name="twitter:description">
  
    <link rel="alternative" href="/atom.xml" title="安生" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  <link rel="stylesheet" href="/css/style.css">
  <link href="//fonts.useso.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <!--[if lt IE 9]><script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7/html5shiv.min.js"></script><![endif]-->
  
</head>
<body>
<div id="container">
  <div id="wrap">
    <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">安生</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="http://www.baidu.com/baidu" method="get" accept-charset="utf-8" class="search-form">
          <input type="search" name="word" maxlength="20" class="search-form-input" placeholder="Search">
          <input type="submit" value="" class="search-form-submit">
          <input name=tn type=hidden value="bds">
          <input name=cl type=hidden value="3">
          <input name=ct type=hidden value="2097152">
          <input type="hidden" name="si" value="localhost">
        </form>
      </div>
    </div>
  </div>
</header>
    <div class="outer">
      <section id="main">
  
    <article id="post-Linux服务器配置与LNMP安装实录-1" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/05/01/Linux服务器配置与LNMP安装实录-1/" class="article-date">
  <time datetime="2015-05-01T12:35:00.000Z" itemprop="datePublished">2015-05-01</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2015/05/01/Linux服务器配置与LNMP安装实录-1/">Linux服务器配置与LNMP安装实录---(1)</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>最近实验室买了新的vps服务器，系统为centos6.6 64位，准备安装lnmp搭建web站点。由于vps是新买的，所以一切都是从新的开始。</p>
<p>好了，简单的记录一下这次服务器搭建过程，整理一下，以备后用。</p>
        
          <p class="article-more-link">
            <a href="/2015/05/01/Linux服务器配置与LNMP安装实录-1/#more">Read More</a>
          </p>
        
      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://localhost/2015/05/01/Linux服务器配置与LNMP安装实录-1/" data-id="cimvrqg1x0003kyfeog0j9gxm" class="article-share-link">分享到</a>
      

      
        <a href="http://localhost/2015/05/01/Linux服务器配置与LNMP安装实录-1/#ds-thread" class="article-comment-link">评论</a>
      

      
    </footer>
  </div>
  
</article>


  
    <article id="post-科学上网，简易教程" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/04/16/科学上网，简易教程/" class="article-date">
  <time datetime="2015-04-16T02:25:00.000Z" itemprop="datePublished">2015-04-16</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2015/04/16/科学上网，简易教程/">科学上网，简易教程</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>帆樯的方法不少，有付费的，比如红杏，有免费的goagent，都算是方法吧。</p>
<p>在这里我介绍两种，一种就是简单的修改hosts，另一种更简单，使用chrome插件，chrome下类似于红杏的插件，实现机制应该是使用代理服务器，这里有免费的。</p>
        
          <p class="article-more-link">
            <a href="/2015/04/16/科学上网，简易教程/#more">Read More</a>
          </p>
        
      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://localhost/2015/04/16/科学上网，简易教程/" data-id="cimvrqg250007kyfebn0cysw7" class="article-share-link">分享到</a>
      

      
        <a href="http://localhost/2015/04/16/科学上网，简易教程/#ds-thread" class="article-comment-link">评论</a>
      

      
    </footer>
  </div>
  
</article>


  
    <article id="post-记Fedora-21-Yun源配置和软件安装" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2014/12/17/记Fedora-21-Yun源配置和软件安装/" class="article-date">
  <time datetime="2014-12-16T20:31:00.000Z" itemprop="datePublished">2014-12-17</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2014/12/17/记Fedora-21-Yun源配置和软件安装/">记Fedora 21 Yun源配置和软件安装</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>因为不小心把CentOS搞崩了，没办法．借着给同学装系统的机会，就把系统重新装了．这个安装的是Fedora21，直接刻u盘，u盘引导启动安装即可，这里不再细说，直接说安装完后．</p>
<p>安装完Fedora，首先，重新配置了一下yum源，这个测试了一下，因为平时在教育网的时间比较多，官方源就试了下清华的．果然，清华的源就是给力，教育网更新速度在20Mbps以上，比较给力．这里直接给出清华源的配置文件，其他的源参照修改即可．</p>
<p>首先是fedora.repo</p>
<pre><code>[fedora] 
name=Fedora $releasever - $basearch - ustc
failovermethod=priority 
baseurl=http://mirrors.tuna.tsinghua.edu.cn/fedora/releases/$releasever/Everything/$basearch/os/ 
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&amp;amp;arch=$basearch 
enabled=1 
metadata_expire=7d 
gpgcheck=1 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch 
#priority=1

[fedora-debuginfo] 
name=Fedora $releasever - $basearch - Debug - ustc
failovermethod=priority 
baseurl=http://mirrors.tuna.tsinghua.edu.cn/fedora/releases/$releasever/Everything/$basearch/debug/ 
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-debug-$releasever&amp;amp;arch=$basearch 
enabled=0 
metadata_expire=7d 
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch 
#priority=1

[fedora-source] 
name=Fedora $releasever - Source - ustc
failovermethod=priority 
baseurl=http://mirrors.tuna.tsinghua.edu.cn/fedora/releases/$releasever/Everything/source/SRPMS/ 
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-source-$releasever&amp;amp;arch=$basearch 
enabled=0 
metadata_expire=7d 
gpgcheck=1 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch 
#priority=1
然后，fedora-updates.repo

[updates]
name=Fedora $releasever - $basearch - Updates - ustc
failovermethod=priority 
baseurl=http://mirrors.tuna.tsinghua.edu.cn/fedora/updates/$releasever/$basearch/ 
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=updates-released-f$releasever&amp;amp;arch=$basearch 
enabled=1 
gpgcheck=1 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch 
#priority=1

[updates-debuginfo] 
name=Fedora $releasever - $basearch - Updates - Debug -ustc
failovermethod=priority 
baseurl=http://mirrors.tuna.tsinghua.edu.cn/fedora/updates/$releasever/$basearch/debug/ 
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=updates-released-debug-f$releasever&amp;amp;arch=$basearch 
enabled=0 
gpgcheck=1 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch 
#priority=1

[updates-source] 
name=Fedora $releasever - Updates Source - ustc
failovermethod=priority 
baseurl=http://mirrors.tuna.tsinghua.edu.cn/fedora/updates/$releasever/SRPMS/ 
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=updates-released-source-f$releasever&amp;amp;arch=$basearch 
enabled=0 
gpgcheck=1 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch 
#priority=1
</code></pre>
      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://localhost/2014/12/17/记Fedora-21-Yun源配置和软件安装/" data-id="cimvrqg260008kyfey2oqh3w1" class="article-share-link">分享到</a>
      

      
        <a href="http://localhost/2014/12/17/记Fedora-21-Yun源配置和软件安装/#ds-thread" class="article-comment-link">评论</a>
      

      
    </footer>
  </div>
  
</article>


  
    <article id="post-Linux运维文档之nginx【转】" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2014/12/09/Linux运维文档之nginx【转】/" class="article-date">
  <time datetime="2014-12-08T21:58:00.000Z" itemprop="datePublished">2014-12-09</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2014/12/09/Linux运维文档之nginx【转】/">Linux运维文档之nginx【转】</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>  1.检查并且安装依赖组件<br>检查安装nginx的依赖性，nginx的模块需要第三方库的支持，检查是否安装下列库：zlib、zlib-devel、openssl、openssl-devel、prce、prce-devel如果没有，则全部装上</p>
<pre><code>yum install zlib zlib-devel openssl openssl-devel prce prce-devel2.安装pcre
`&lt;/pre&gt;

&lt;pre&gt;`wget http://xiyoulinux.qiniudn.com/pcre-8.36.tar.gz
tar -xzvf pcre-8.36.tar.gz -C ../software/
cd ../software/pcre-8.36/
./configure
make &amp;amp;&amp;amp; make install
`&lt;/pre&gt;

  3.安装google-perftools

&lt;pre&gt;`wget http://xiyoulinux.qiniudn.com/libunwind-1.1.tar.gz     
wget http://xiyoulinux.qiniudn.com/gperftools-2.3.tar.gz
tar -xzvf libunwind-1.1.tar.gz -C ../software/
tar -xzvf gperftools-2.3.tar.gz -C ../software/
cd ../software/llibunwind-1.1/
./configure
make
make install
cd ../gperftools-2.3/
./configure
make &amp;amp;&amp;amp; make install
`&lt;/pre&gt;

4.编译安装NGINX

先建nginx的启动用户

useradd –s /sbin/nologin nginx

wget http://nginx.org/download/nginx-1.7.8.tar.gz

tar -xzvf nginx-1.7.8.tar.gz -C ../software/

cd ../software/nginx-1.7.8/

&lt;pre&gt;`./configure –with-cc-opt=’-O3′ \ # 注意整个不是零，是大写英文字母O&amp;amp;nbsp;
–with-google_perftools_module \ # 可选组件
–prefix=/usr/local/nginx \ # nginx安装目录
–with-openssl=/usr/lib \
–with-http_stub_status_module \
–with-http_image_filter_module \
–user=nginx \
–group=nginx

make &amp;amp;&amp;amp; make install修改一下配置：

grep nginx /usr/local/nginx/conf/nginx.conf

user nginx nginx;要点：禁止DEBUG模式
</code></pre><h1 id="vi-auto-cc-gcc"><a href="#vi-auto-cc-gcc" class="headerlink" title="vi auto/cc/gcc"></a>vi auto/cc/gcc</h1><h1 id="debug-注释下面"><a href="#debug-注释下面" class="headerlink" title="debug //注释下面"></a>debug //注释下面</h1><p>CFLAGS=”$CFLAGS -g”</p>
<p><span style="font-size:16px;">5.nginx的信号控制(有关nginx的启动与关闭)</span></p>
<p>TERM,INT 快速关闭QUIT 从容关闭</p>
<p>HUP 平滑重启，重新加载配置文件</p>
<p>USR1 重新打开日志文件，在切割日志时用途较大；</p>
<p>USR2 平滑升级可执行程序</p>
<p>WINCH 从容关闭工作进程</p>
<p>我们可以直接通过以下命令来完成平滑重启，省下寻找nginx主进程号的步骤；</p>
<p>kill -“信号类型”‘/usr/local/nginx/logs/nginx.pid’或者nginx的主进程号</p>
<p>(1）、从容停止nginx</p>
<p>kill -QUIT 6019 #nginx主进程号</p>
<p>kill -QUIT <code>cat /usr/local/nginx/logs/nginx.pid</code></p>
<p>(2)、快速停止nginx</p>
<p>kill -TERM /INT nginx主进程号</p>
<p>kill -TERM /INT `cat /usr/local/nginx/logs/nginx.pid</p>
<p>(3)、强制停止所有的nginx进程</p>
<p>pkill -9 nginx</p>
<p>(4)、平滑重启nginx</p>
<p>修改了nginx的配置文件要重启nginx；重启之前要检查配置文件是否正确：</p>
<h1 id="usr-local-nginx-sbin-nginx-t-c-usr-local-nginx-conf-nginx-conf"><a href="#usr-local-nginx-sbin-nginx-t-c-usr-local-nginx-conf-nginx-conf" class="headerlink" title="/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf"></a>/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf</h1><p>the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok</p>
<p>configuration file /usr/local/nginx/conf/nginx.conf test is successful</p>
<h1 id="kill-HUP-cat-usr-local-nginx-logs-nginx-pid"><a href="#kill-HUP-cat-usr-local-nginx-logs-nginx-pid" class="headerlink" title="kill -HUP cat /usr/local/nginx/logs/nginx.pid"></a>kill -HUP <code>cat /usr/local/nginx/logs/nginx.pid</code></h1><p>nginx的平滑重启</p>
<p>kll HUP Nginx主进程号</p>
<p>或者</p>
<p>kll HUP nginx.pid文件按存放路径•</p>
<p>  <span style="color:#555555;font-family: 宋体, Arial, 'Microsoft Yahei', sans-serif; font-size: 14px; line-height: 20.2999992370605px;">NGINX基本配置</span><br><span style="font-size:12px;">Nginx.conf配置文件：<br>//以下为配置内容<br>user nginx; # 指定运行nginx的用户和组<br>worker_processes 2; # 工作进程数,基本为CPU的核心数或者两倍</span></p>
<h1 id="指定全局错误日志的路径，错误日志可选项-有-debug-info-notice-warn-error-crit"><a href="#指定全局错误日志的路径，错误日志可选项-有-debug-info-notice-warn-error-crit" class="headerlink" title="指定全局错误日志的路径，错误日志可选项 有[debug|info|notice|warn|error|crit]"></a>指定全局错误日志的路径，错误日志可选项 有[debug|info|notice|warn|error|crit]</h1><p>error_log logs/error.log info;<br>pid logs/nginx.pid; # 指定pid文件位置<br>events {<br>worker_connections 1024; # 最大连接数<br>}<br>http {<br>include mime.types; # 设定mime类型<br>default_type application/octet-stream;<br>//定义日志格式</p>
<p>#log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘</p>
<h1 id="‘-status-body-bytes-sent-“-http-referer”-‘"><a href="#‘-status-body-bytes-sent-“-http-referer”-‘" class="headerlink" title="‘$status $body_bytes_sent “$http_referer” ‘"></a>‘$status $body_bytes_sent “$http_referer” ‘</h1><h1 id="‘”-http-user-agent”-“-http-x-forwarded-for”‘"><a href="#‘”-http-user-agent”-“-http-x-forwarded-for”‘" class="headerlink" title="‘”$http_user_agent” “$http_x_forwarded_for”‘;"></a>‘”$http_user_agent” “$http_x_forwarded_for”‘;</h1><p>access_log logs/access.log main; # 访问日志目录以及格式<br>sendfile on; # sendfile有效提高web文件传输速度</p>
<p>#tcp_nopush on;</p>
<p>#keepalive_timeout 0;<br>keepalive_timeout 65;<br>gzip on;</p>
<p>#站点配置<br>server {<br>listen 80;<br>server_name localhost;</p>
<p>#charset koi8-r;</p>
<p>#access_log logs/host.access.log main;<br>//定义主目录,类似apache的DocumentRoot<br>location / {<br>root /nginx_www; # 网站根目录<br>index index.html index.htm; # 默认首页<br>}<br>error_page 404 /404.html; # 404错误页面<br>error_page 500 502 503 504 /50x.html; # 将500错误转到50x.html上<br>location = /50x.html { # 如果访问的页面等于50x.html,则从html目录下找<br>root /nginx_www;<br>}<br>}<br>}<br>首先有个全局的配置<br>然后配置一个httpd段<br>httpd配置段里面包含多个server段,也就是常说的虚拟主机<br>server段里面可以配置各个站点特有的配置</p>
<p>  <span style="font-size:12px;">NGINX每个进程配置一个CPU<br>nginx进程设置方法, worker_processes 1;<br>查看CPU个数:<br>cat /proc/cpuinfo | grep processor<br>配置1:4 CPU (4 Core) + 4 worker_processes (每个worker_processes 使用1个CPU)<br>orker_processes 4;<br>orker_cpu_affinity 0001 0010 0100 1000;<br>配置2:8 CPU (8 Core) + 8 worker_processes (每个worker_processes 使用1个CPU)<br>orker_processes 8;<br>orker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;<br>配置3:16 CPU (16 Core) + 16 worker_processes (每个worker_processes 使用1个CPU)<br>orker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;</span></p>
<p>  <span style="font-size:12px;">基于域名的虚拟主机配置<br>1.两个站点分别为：Web1.ttlsa.com、Web2.ttlsa.com<br>搭建web1主目录、搭建web2主目录</span></p>
<h1 id="mkdir-nginx-www-web1-ttlsa-com"><a href="#mkdir-nginx-www-web1-ttlsa-com" class="headerlink" title="mkdir /nginx_www/web1.ttlsa.com"></a>mkdir /nginx_www/web1.ttlsa.com</h1><h1 id="echo-“web1-ttlsa-com”-gt-nginx-www-index-html"><a href="#echo-“web1-ttlsa-com”-gt-nginx-www-index-html" class="headerlink" title="echo “web1.ttlsa.com” &gt; /nginx_www/index.html"></a>echo “web1.ttlsa.com” &gt; /nginx_www/index.html</h1><h1 id="mkdir-nginx-www-nginx-www-web2-ttlsa-com"><a href="#mkdir-nginx-www-nginx-www-web2-ttlsa-com" class="headerlink" title="mkdir /nginx_www//nginx_www/web2.ttlsa.com"></a>mkdir /nginx_www//nginx_www/web2.ttlsa.com</h1><h1 id="echo-“web2-ttlsa-com”-gt-nginx-www-web2-ttlsa-com-index-html"><a href="#echo-“web2-ttlsa-com”-gt-nginx-www-web2-ttlsa-com-index-html" class="headerlink" title="echo “web2.ttlsa.com” &gt; /nginx_www/ web2.ttlsa.com/index.html"></a>echo “web2.ttlsa.com” &gt; /nginx_www/ web2.ttlsa.com/index.html</h1><p>在http配置段里增加如下配置：<br>server {<br>listen 80;<br>server_name web1.ttlsa.com;<br>location / {<br>root /nginx_www/web1.ttlsa.com; # 网站根目录<br>index index.html index.htm; # 默认首页<br>}<br>error_page 404 /404.html; # 404错误页面<br>error_page 500 502 503 504 /50x.html;<br>}<br>server {<br>listen 80;<br>server_name web2.ttlsa.com;<br>location / {<br>root /nginx_www/web2.ttlsa.com; # 网站根目录<br>index index.html index.htm; # 默认首页<br>}<br>error_page 404 /404.html; # 404错误页面<br>error_page 500 502 503 504 /50x.html;<br>}<br>2.开始做个本地测试，先要修改<a href="http://www.ttlsa.com/windows/" title="windows" target="_blank" rel="external">windows</a>下的hosts文件,加入如下配置<br>C:\Windows\System32\drivers\etc\hosts<br>192.168.1.203 web1.ttlsa.com<br>192.168.1.203 web2.ttlsa.com</p>
<p>  <span style="font-size:12px;">NGINX配置文件过期时间expires</span></p>
<h1 id="参数off禁止修改应答头中的”Expires”和”Cache-Control”。"><a href="#参数off禁止修改应答头中的”Expires”和”Cache-Control”。" class="headerlink" title="参数off禁止修改应答头中的”Expires”和”Cache-Control”。"></a>参数off禁止修改应答头中的”Expires”和”Cache-Control”。</h1><h1 id="注意：expires仅仅适用于200-204-301-302-和304应答"><a href="#注意：expires仅仅适用于200-204-301-302-和304应答" class="headerlink" title="注意：expires仅仅适用于200, 204, 301, 302,和304应答"></a>注意：expires仅仅适用于200, 204, 301, 302,和304应答</h1><p>1.根据文件类型配置(大部分情况下是这么配置的)<br>对图片，flash文件在浏览器本地缓存30天<br>expires on # 启用设置expire过期时间<br>location ~ .<em>.(gif|jpg|jpeg|png|bmp|swf)$<br>{<br>expires 30d;<br>}<br>对js，css文件在浏览器本地缓存1小时<br>location ~ .</em>.(js|css)$<br>{<br>expires 1h;<br>}<br>2.根据目录来设置<br>location ~ ^/(image|js|static|flash)/{<br>root /nginx_www/down; # 匹配这些url主目录都在down下,并且过期时间为30<br>expires 30d;<br>}</p>
<p>  <span style="font-size:12px;">NGINX配置禁止访问某类文件<br>1.使用break<br>location ~ .*.(exe|doc|rar)$<br>{<br>if (-f $request_filename) # 注意if和(之间有个空格<br>{<br>root /nginx/x; #当名字匹配时,把网站根目录替换成其他目录,使得用户无法下载<br>break; # 当名字匹配时,直接跳出不处理<br>}<br>}<br>范例一：假如说我有一个txt文件在web1.ttlsa.com上,我不让别人访问,那么怎么做呢?<br>现在目录下建一个txt文件</span></p>
<h1 id="vi-nginx-www-web1-ttlsa-com-a-txt"><a href="#vi-nginx-www-web1-ttlsa-com-a-txt" class="headerlink" title="vi /nginx_www/web1.ttlsa.com/a.txt"></a>vi /nginx_www/web1.ttlsa.com/a.txt</h1><p>最后修改一下nginx.conf配置,Server段配置段如下：<br>server{<br>listen 80;<br>server_name web1.ttlsa.com;<br>location / {<br>root /nginx_www/web1.ttlsa.com;<br>index index.html index.htm;<br>}<br>error_page 404 /404.html;<br>location ~ .txt$ {<br>if (-f $request_filename)<br>{</p>
<p>#root /nginx/xxx;<br>break;<br>}<br>}<br>}<br>接着我们来测试访问,可以看到提示404 Not Found<br>2.使用deny<br>location ~ .*.(exe|doc|rar)$<br>{<br>root /nginx/x; #当名字匹配时,把网站根目录替换成其他目录,使得用户无法下载<br>deny all;<br>}</p>
<p>  <span style="font-size:12px;">禁止访问某个目录<br>location ~ ^/(admin)/ {<br>deny all; # 所有访问/admin目录的URL都被拒绝掉<br>}</span></p>
<p>  <span style="font-size:12px;">限制某些ip访问<br>location / {<br>deny 192.168.1.1;<br>deny 192.168.2.0/24;<br>allow all; # 上面呢两个被拒绝,允许其他所有的ip<br>}</span></p>
<p>  <span style="font-size:12px;">nginx下载连接数限制、速度限制<br>1.limit_zone<br>语法：limit_zone zone_name $variable memory_max_size<br>默认值：no<br>使用字段：http<br>指令描述会话状态存储区域。<br>会话的数目按照指定的变量来决定，它依赖于使用的变量大小和memory_max_size的值。<br>如下例：<br>limit_zone one $binary_remote_addr 10m;<br>客户端的地址将用于会话，注意$binary_remote_addr变量将替换$remote_addr而被使用。<br>$remote_addr 变量的值的长度可以是7到15字节，因此大小指定为32或64字节。<br>$binary_remote_addr 变量的值的长度总是4字节，大小总是32字节。<br>当会话状态储存区域为1M时理论上可以处理32000个会话，每个会话大小为32字节。<br>2.limit_conn<br>语法：limit_conn zone_name max_clients_per_ip<br>默认值：no<br>使用字段：http, server, location<br>指令指定一个会话的最大同时连接数，超过这个数字的请求将被返回”Service unavailable” (503)代码。<br>如下例：<br>limit_zone one $binary_remote_addr 10m; # 使用10MB来存储会话,可存32w个会话<br>Server{<br>Listen 80;<br>Server_name download.ttlsa.com;<br>Index index.html index.html index.<a href="http://www.ttlsa.com/php/" title="php" target="_blank" rel="external">php</a>;</span></p>
<p>#Zone limit<br>Location / {<br>limit_conn one 1; # 值允许一个连接<br>limit_rate 20k; # 一个连接最大20k速度<br>}<br>}<br>接下来我们测试一下效果，我在web1.ttlsa.com根目录下传了一个飞信的安装包，20多MB，看看速度是多少吧，接近20KB/秒.<br>这次把速度开到50KB，连接数还是1，配置段如下<br>limit_conn one 1;<br>limit_rate 50k;<br>连接数改成10，速率到50KB，速度应该达到250KB/秒左右了吧<br>limit_conn one 10;<br>limit_rate 50k;</p>
<p>  <span style="font-size:12px;">Nginx列出目录下的列表-目录索引<br>整个站点<br>location / {<br>root /nginx/web1.ttlsa.com<br>autoindex on;<br>}<br>也可以单个目录<br>location / {<br>root /nginx/web1.ttlsa.com/list; # 要索引的目录<br>autoindex on; # 打开索引<br>}<br>接下来看看整个列子，把web1.ttlsa.com的list索引目录列出,配置如下<br>server{<br>listen 80;<br>server_name web1.ttlsa.com;<br>location / {<br>root /nginx_www/web1.ttlsa.com;<br>index index.html index.htm;<br>}<br>error_page 404 /404.html;<br>location /list<br>{<br>root /nginx_www/web1.ttlsa.com;<br>autoindex on;<br>默认关闭状态<br>autoindex_exact_size off;<br>默认为on，显示出文件的确切大小，单位是bytes。<br>改为off后，显示出文件的大概大小，单位是kB或者MB或者GB<br>autoindex_localtime on;<br>默认为off，显示的文件时间为GMT时间。<br>改为on后，显示的文件时间为文件的服务器时间<br>}<br>}</span></p>
<p>  <span style="font-size:12px;">NGINX日志处理<br>设置一个计划任务，每天12点把access.log剪切到一个目录下，并且从命名为响应的名字</span></p>
<p>  <span style="font-size:12px;">NGINX忽略部分日志<br>location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$<br>{<br>access_log off; # 列出的这些图片格式不记录日志<br>}</span></p>
<p>  <span style="font-size:12px;">NGINX反向代理配置<br>nginx.conf配置文件：<br>user nobody nobody;<br>worker_processes 4;<br>error_log logs/error.log crit;<br>pid logs/nginx.pid;<br>worker_rlimit_nofile 65535;<br>events {<br>use epoll;<br>worker_connections 65535;<br>}<br>http {<br>server_tokens off;<br>include mime.types;<br>default_type application/octet-stream;<br>server_names_hash_bucket_size 128;<br>sendfile on;<br>tcp_nopush on;<br>tcp_nodelay on;<br>keepalive_timeout 65;<br>gzip on;<br>gzip_min_length 1k;<br>gzip_buffers 4 16k;<br>gzip_http_version 1.0;<br>gzip_comp_level 2;<br>gzip_types text/plain application/x-javascript text/css application/xml;<br>gzip_vary on;<br>upstream mysrv {<br>server 192.168.1.1:80 weight=1 max_fails=2 fail_timeout=30s;<br>server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s;<br>}<br>upstream bench {<br>server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=30s;<br>server 192.168.1.4:80 weight=1 max_fails=2 fail_timeout=30s;<br>}<br>upstream bbs {<br>server 192.168.1.5:80 weight=1 max_fails=2 fail_timeout=30s;<br>server 192.168.1.6:80 weight=1 max_fails=2 fail_timeout=30s;<br>}<br>include vhost/<em>.conf;<br>}<br>aaa_example_com.conf配置文件：<br>server<br>{<br>listen 80;<br>server_name aaa.example.com;<br>index index.php index.html index.htm index.shtml;<br>log_format proxy ‘$remote_addr| $upstream_addr| $connection| $upstream_status| $time_local| $request|’<br>‘$status| $body_bytes_sent| $bytes_sent| $http_referer|’<br>‘ $http_user_agent| $upstream_response_time| $msec| $request_time’;<br>access_log logs/aaa_access.log proxy;<br>location /<br>{<br>proxy_pass <a href="http://mysrv" target="_blank" rel="external">http://mysrv</a>; # 当访问aaa.example.com,默认解析转发到后端的mysrv<br>include proxy.conf;<br>}<br>location /bench/<br>{<br>proxy_pass <a href="http://bench" target="_blank" rel="external">http://bench</a>; #当访问/bench/转发到upstream配置的bench下<br>I nclude proxy.conf;<br>}<br>}<br>bbs_example_com.conf配置文件：<br>server<br>{<br>listen 80;<br>server_name bbs.example.com </em>.bbs.example.com;<br>log_format proxy ‘$remote_addr| $upstream_addr| $connection| $upstream_status| $time_local| $request|’<br>‘ $status| $body_bytes_sent| $bytes_sent| $http_referer|’<br>‘ $http_user_agent| $upstream_response_time| $msec| $request_time’;<br>access_log logs/bbs_access.log proxy;<br>location /<br>{<br>proxy_pass <a href="http://bbs" target="_blank" rel="external">http://bbs</a>;<br>include proxy.conf;<br>}<br>}<br>proxy.conf配置文件：<br>proxy_redirect off;<br>proxy_set_header Host $host;<br>proxy_set_header X-Real-IP $remote_addr;<br>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br>client_max_body_size 50m; # 允许客户端请求的最大单个文件字节数<br>client_body_buffer_size 256k; # 缓冲区代理缓冲客户端请求的最大字节数<br>proxy_connect_timeout 30; # 连接后端服务器超时时间<br>proxy_send_timeout 30; # 后端服务器发送数据超时时间,连接以及建立<br>proxy_read_timeout 60; # 后端服务器响应请求超时时间,从开始发送到接受完毕<br>proxy_buffer_size 4k; # 代理请求缓存区大小<br>proxy_buffers 4 32k;<br>proxy_busy_buffers_size 64k; #系统繁忙时可申请的proxy_buffers大小<br>proxy_temp_file_write_size 64k; #proxy缓存临时文件的大小<br>proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;</span></p>
<h1 id="故障转移"><a href="#故障转移" class="headerlink" title="故障转移"></a>故障转移</h1><p>proxy_max_temp_file_size 128m;<br>proxy_set_header指令用于在向反向代理的后端web服务器发起请求时添加指定Header头信息，当后端web服务器上有多个基于域名的虚拟主机时，要通过添加Header头信息Host，来指定请求的域名，这样后端web服务器才能识别该反向代理访问请求由哪个虚拟主机来处理。</p>
<p>  <span style="font-size:12px;">Nginx缓存服务器配置</span></p>
<h1 id="wget-http-labs-frickle-com-files-ngx-cache-purge-1-3-tar-gz-清缓存模块"><a href="#wget-http-labs-frickle-com-files-ngx-cache-purge-1-3-tar-gz-清缓存模块" class="headerlink" title="wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz //清缓存模块"></a>wget <a href="http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz" target="_blank" rel="external">http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz</a> //清缓存模块</h1><h1 id="tar-zxvpf-ngx-cache-purge-1-3-tar-gz-C-software"><a href="#tar-zxvpf-ngx-cache-purge-1-3-tar-gz-C-software" class="headerlink" title="tar zxvpf ngx_cache_purge-1.3.tar.gz -C ../software/"></a>tar zxvpf ngx_cache_purge-1.3.tar.gz -C ../software/</h1><h1 id="cd-usr-local-src-software-nginx-1-0-2"><a href="#cd-usr-local-src-software-nginx-1-0-2" class="headerlink" title="cd /usr/local/src/software/nginx-1.0.2"></a>cd /usr/local/src/software/nginx-1.0.2</h1><h1 id="configure-–user-nobody-–group-nobody-–prefix-usr-local-nginx-1-0-2-–with-http-stub-status-module-–with-http-ssl-module-–add-module-ngx-cache-purge-1-3"><a href="#configure-–user-nobody-–group-nobody-–prefix-usr-local-nginx-1-0-2-–with-http-stub-status-module-–with-http-ssl-module-–add-module-ngx-cache-purge-1-3" class="headerlink" title="./configure –user=nobody –group=nobody –prefix=/usr/local/nginx-1.0.2 –with-http_stub_status_module –with-http_ssl_module –add-module=../ngx_cache_purge-1.3"></a>./configure –user=nobody –group=nobody –prefix=/usr/local/nginx-1.0.2 –with-http_stub_status_module –with-http_ssl_module –add-module=../ngx_cache_purge-1.3</h1><h1 id="mkdir-p-www-nginx-proxy-temp-path"><a href="#mkdir-p-www-nginx-proxy-temp-path" class="headerlink" title="mkdir -p /www/nginx/proxy_temp_path"></a>mkdir -p /www/nginx/proxy_temp_path</h1><h1 id="mkdir-p-www-nginx-proxy-cache-path"><a href="#mkdir-p-www-nginx-proxy-cache-path" class="headerlink" title="mkdir -p /www/nginx/proxy_cache_path"></a>mkdir -p /www/nginx/proxy_cache_path</h1><p>nginx.conf配置文件:<br>user nobody nobody;<br>worker_processes 4;<br>error_log logs/error.log crit;<br>pid logs/nginx.pid;<br>worker_rlimit_nofile 65535;<br>events {<br>use epoll;<br>worker_connections 65535;<br>}<br>http {<br>server_tokens off;<br>include mime.types;<br>default_type application/octet-stream;<br>server_names_hash_bucket_size 128;</p>
<p>  <span style="font-size:12px;">sendfile on;<br>tcp_nopush on;<br>tcp_nodelay on;<br>keepalive_timeout 65;<br>gzip on;<br>gzip_min_length 1k;<br>gzip_buffers 4 16k;<br>gzip_http_version 1.0;<br>gzip_comp_level 2;<br>gzip_types text/plain application/x-javascript text/css application/xml;<br>gzip_vary on;<br>upstream mysrv {<br>server 192.168.1.1:80 weight=1 max_fails=2 fail_timeout=30s;<br>server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s;<br>}<br>include vhost/<em>.conf;<br>}<br>aaa_example_com.conf配置文件：<br>server {<br>listen 80;<br>server_name aaa.example.com;<br>index index.php index.html index.htm index.shtml;<br>log_format proxy ‘$remote_addr| $upstream_addr| $connection| $upstream_status| $time_local| $request|’<br>‘ $status| $body_bytes_sent| $bytes_sent| $http_referer|’<br>‘ $http_user_agent| $upstream_response_time| $msec| $request_time’;<br>access_log logs/aaa_access.log proxy;<br>location /<br>{<br>proxy_pass <a href="http://mysrv" target="_blank" rel="external">http://mysrv</a>;<br>proxy_set_header Host $host;<br>proxy_set_header X-Forwarded-For $remote_addr;<br>}<br>location ~ .</em>.(gif|jpg|jpeg|png|bmp|swf|js|css)$<br>{<br>proxy_pass <a href="http://mysrv" target="_blank" rel="external">http://mysrv</a>;<br>include proxy.conf;<br>}<br>location ~ /purge(/.*)<br>{<br>allow 127.0.0.1;<br>allow 192.168.1.0/24;<br>deny all;<br>proxy_cache_purge cache_one $host$1$is_args$args;<br>}<br>}<br>proxy.conf配置文件：<br>proxy_redirect off;<br>proxy_set_header Host $host;<br>proxy_set_header X-Real-IP $remote_addr;<br>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br>client_max_body_size 50m; //允许客户端请求的最大单个文件字节数<br>client_body_buffer_size 256k; //缓冲区代理缓冲客户端请求的最大字节数<br>proxy_connect_timeout 30; //连接后端服务器超时时间<br>proxy_send_timeout 30; //后端服务器发送数据超时时间<br>proxy_read_timeout 60; //后端服务器响应请求超时时间<br>proxy_buffer_size 4k; //代理请求缓存区大小<br>proxy_buffers 4 32k;<br>proxy_busy_buffers_size 64k; //系统繁忙时可申请的proxy_buffers大小<br>proxy_temp_file_write_size 64k; //proxy缓存临时文件的大小<br>proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;<br>//故障转移<br>proxy_max_temp_file_size 128m;<br>proxy_temp_path /www/nginx/proxy_temp_path;<br>proxy_cache_path /www/nginx/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=1g; //设置web缓存区名称为cache_one，内存缓存空间为200m，自动清除超过1天没有被访问的缓存数据，硬盘缓存空间为1g<br>proxy_cache cache_one; //使用web缓存区cache_one<br>proxy_cache_valid 200 304 12h;<br>proxy_cache_valid 301 302 1m;<br>proxy_cache_valid any 1m;<br>proxy_cache_key $host$uri$is_args$args; //设置web缓存的key值，nginx根据key值md5哈希存储缓存</span></p>
<div><br><br><em> </em> *<br><br></div><br><div><br>  <span style="color: rgb(85, 85, 85); font-family: 宋体, Arial, 'Microsoft Yahei', sans-serif; font-size: 14px; line-height: 20.2999992370605px;">文章标题: </span><a href="http://www.ttlsa.com/nginx/nginx-document/" target="_blank" rel="external">Linux运维文档之nginx</a><br><span style="color: rgb(85, 85, 85); font-family: 宋体, Arial, 'Microsoft Yahei', sans-serif; font-size: 14px; line-height: 20.2999992370605px;">原文地址: </span><a href="http://www.ttlsa.com/nginx/nginx-document/" title="Linux运维文档之nginx" target="_blank" rel="external">http://www.ttlsa.com/nginx/nginx-document/</a><br><br></div><br><div id="xunlei_com_thunder_helper_plugin_d462f475-c18e-46be-bd10-327458d045bd" style="margin: 0px; padding: 0px; color: rgb(85, 85, 85); font-family: 宋体, Arial, 'Microsoft Yahei', sans-serif; font-size: 14px; line-height: 20.2999992370605px;"><br></div>
      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://localhost/2014/12/09/Linux运维文档之nginx【转】/" data-id="cimvrqg1v0002kyfea8egd46n" class="article-share-link">分享到</a>
      

      
        <a href="http://localhost/2014/12/09/Linux运维文档之nginx【转】/#ds-thread" class="article-comment-link">评论</a>
      

      
    </footer>
  </div>
  
</article>


  
    <article id="post-CentOS-7安装完第一步：yum源配置" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2014/11/26/CentOS-7安装完第一步：yum源配置/" class="article-date">
  <time datetime="2014-11-25T22:36:00.000Z" itemprop="datePublished">2014-11-26</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2014/11/26/CentOS-7安装完第一步：yum源配置/">CentOS 7安装完第一步：yum源配置</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p><span style="font-size:10.5pt;font-family:Arial;"><span style="white-space:pre"> </span>最近刚刚把系统换成<span style="font-family:Arial;">CentOS 7</span><span style="font-family:宋体;">，搜索了一下，关于</span><span style="font-family:Arial;">CentOS7</span><span style="font-family:宋体;">的新手入门的文章不多，就整理了一下我装完</span><span style="font-family:Arial;">CentOS</span><span style="font-family:宋体;">后做的一些东西，一边是为自己以后查询方便，一边给大家作参考。有问题欢迎指出，谢谢。</span></span></p>
<p><span style="font-size:10.5pt;font-family:Arial;"><span style="white-space:pre"> </span>首先，装完<span style="font-family:Arial;">CentOS</span><span style="font-family:宋体;">，肯定是先配置</span><span style="font-family:Arial;">yum</span><span style="font-family:宋体;">源，关闭不用的服务软件，安装一些常用的软件。</span></span></p>
<p><span style="font-size:10.5pt;font-family:Arial;"><span style="white-space:pre"> </span>好，开始第一步，配置<span style="font-family:Arial;">yum</span><span style="font-family:宋体;">源。</span></span></p>
<p><span style="font-size:10.5pt;font-family:Arial;">官方源：</span></p>
<p>  <span style="font-size:10.5pt;font-family:Arial;"><span style="white-space:pre"> </span>CentOS<span style="font-family:宋体;">自带了四个官方的源，默认的打开的有</span><span style="font-family:Arial;">base,updates</span><span style="font-family:宋体;">和</span><span style="font-family:Arial;">extras</span><span style="font-family:宋体;">，官方源里的软件兼容性和稳定性是最好的（</span><span style="font-family:Arial;">ps:</span><span style="font-family:宋体;">貌似这是废话。。。），当然呢，官方源中也去除了很多有版权争议的软件，安装的软件也不一定不是最新的。由于刚刚安装好的系统都是直接用的官方服务器进行软件下载升级，速度是个问题。所以呢，我们可以更换为国内的镜像服务器。</span></span></p>
<p>  <span style="font-size:10.5pt;font-family:Arial;"><span style="white-space:pre"> </span>方法很简单，首先先备份系统的那啥，</span><span style="color:rgb(51, 51, 51);font-size:10.5pt;font-family:'Helvetica Neue';"><strong>CentOS-Base.repo</strong> </span><span style="font-size:10.5pt;font-family:Arial;">。</span></p>
<pre><code>sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
`&lt;/pre&gt;

&lt;span style=&quot;font-size:10.5pt;font-family:Arial;&quot;&gt;&lt;span style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;然后呢，我使用的是中国科学技术大学提供的镜像，比较懒，直接&lt;span style=&quot;font-family:Arial;&quot;&gt;down&lt;/span&gt;&lt;span style=&quot;font-family:宋体;&quot;&gt;的他们的配置文件。&lt;/span&gt;&lt;/span&gt;

  &lt;span style=&quot;font-size:10.5pt;font-family:Arial;&quot;&gt;&lt;span style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;直接把国内的几个都贴上来:&lt;/span&gt;
</code></pre><ul>
<li><span style="font-family: Arial; font-size: 9pt;">网易的源：</span><span style="font-family: Arial; font-size: 9pt;"><a href="http://mirrors.163.com/centos/" target="_blank" rel="external">http://mirrors.163.com/centos/</a></span></li>
<li><span style="font-family: Arial; font-size: 9pt;">阿里的源：</span><span style="font-family: Arial; font-size: 9pt;"><a href="http://mirrors.aliyun.com/centos/" target="_blank" rel="external">http://mirrors.aliyun.com/centos/</a></span></li>
<li><span style="font-family: Arial; font-size: 9pt;">搜狐的源：</span><span style="font-family: Arial; font-size: 9pt;"><a href="http://mirrors.sohu.com/centos/" target="_blank" rel="external">http://mirrors.sohu.com/centos/</a></span></li>
<li><span style="font-family: Arial; font-size: 9pt;">上海交大：</span><span style="font-family: Arial; font-size: 9pt;"><a href="http://ftp.sjtu.edu.cn/centos/" target="_blank" rel="external">http://ftp.sjtu.edu.cn/centos/</a></span></li>
<li><p><span style="font-family: Arial; font-size: 9pt;">中国科学技术大学的：</span><span style="font-family: Arial; font-size: 9pt;"><a href="http://mirrors.ustc.edu.cn/centos/" target="_blank" rel="external">http://mirrors.ustc.edu.cn/centos/</a></span></p>
<p>  <span style="font-size:10.5pt;font-family:Arial;"><span style="white-space:pre"> </span>使用方法，懒得改，直接<span style="font-family:Arial;">down</span><span style="font-family:宋体;">镜像站提供的配置文件然后复制到配置文件所在目录。这里，以中国科学技术大学的为例（</span><span style="font-family:Arial;">ps</span><span style="font-family:宋体;">：因为它直接提供了</span><span style="font-family:Arial;">CentOS7</span><span style="font-family:宋体;">的配置文件，偷笑</span><span style="font-family:Arial;">ing</span><span style="font-family:宋体;">）</span></span></p>
<p>  <span style="font-size:10.5pt;font-family:Arial;"><span style="white-space:pre"> </span>如果要改为其他的源，</span><span style="font-size:9pt;font-family:Arial;">直接改对应的<span style="font-family:Arial;">baseurl</span><span style="font-family:宋体;">即可。</span></span></p>
<pre>`# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&amp;arch=$basearch&amp;repo=os
baseurl=http://mirrors.ustc.edu.cn/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
# mirrorlist=http://mirrorlist.centos.org/?release=$releasever&amp;arch=$basearch&amp;repo=updates
baseurl=http://mirrors.ustc.edu.cn/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
# mirrorlist=http://mirrorlist.centos.org/?release=$releasever&amp;arch=$basearch&amp;repo=extras
baseurl=http://mirrors.ustc.edu.cn/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
# mirrorlist=http://mirrorlist.centos.org/?release=$releasever&amp;arch=$basearch&amp;repo=centosplus
baseurl=http://mirrors.ustc.edu.cn/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
`</pre>

<p><span style="font-size:10.5pt;font-family:Arial;"><span style="white-space:pre"> </span>先把这个文件内容复制保存为</span><span style="font-size:9pt;font-family:宋体;">CentOS-Base.repo</span><span style="font-size:9pt;font-family:Arial;">在电脑上，<span style="font-family:Arial;">bash</span><span style="font-family:宋体;">切到你保存的目录，然后</span></span></p>
<p><span style="font-size:9pt;font-family:Arial;"><span style="font-family:宋体;"></span></span></p>
<pre>`sudo mv CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
sudo yum makecache
`</pre>

<p>  <span style="font-size:10.5pt;font-family:Arial;"><span style="white-space:pre"> </span>就好了。</span></p>
<p>  <span style="font-size:10.5pt;font-family:Arial;"><span style="white-space:pre"> </span>发现一个不错的网站，提供了一大堆国内<span style="font-family:Arial;">CentOS</span><span style="font-family:宋体;">镜像，链接在这里：</span></span><a href="http://www.centoscn.com/yunwei/news/2012/1227/131.html" target="_blank" rel="external"><span style="color:rgb(0, 0, 255);font-size:10.5pt;font-family:Arial;">中国<span style="font-family:Arial;">linux</span><span style="font-family:宋体;">开源镜像站大全</span></span></a></p>
<p>  <span style="font-size:10.5pt;font-family:Arial;">第三方源：</span></p>
<p>  <span style="font-size:10.5pt;font-family:Arial;"><span style="white-space:pre"> </span>EPEL<span style="font-family:宋体;">源</span></span></p>
<p>  <span style="font-size: 10.5pt;"><span style="font-family: Arial; white-space: pre;"> </span><span style="font-family:Arial;">企业版</span><span style="font-family: Arial;">Linux</span><span style="font-family: 宋体;">附加软件包</span><span style="font-family: Arial;">(</span><span style="font-family: 宋体;">以下简称</span><span style="font-family: Arial;">EPEL)</span><span style="font-family: 宋体;">是一个由特别兴趣小组创建、维护并管理的，针对红帽企业版</span><span style="font-family: Arial;">Linux(RHEL)</span><span style="font-family: 宋体;">及其衍生发行版</span><span style="font-family: Arial;">(</span><span style="font-family: 宋体;">比如</span><span style="font-family: Arial;">CentOS、</span><span style="font-family: Arial;">Scientific Linux</span><span style="font-family:宋体;">、</span><span style="font-family: Arial;">Oracle Enterprise Linux)</span><span style="font-family: 宋体;">的一个高质量附加软件包项目。</span></span></p>
<p>  <span style="font-size:10.5pt;font-family:Arial;"><span style="white-space:pre"> </span>EPEL<span style="font-family:宋体;">的软件包通常不会与企业版</span><span style="font-family:Arial;">Linux</span><span style="font-family:宋体;">官方源中的软件包发生冲突，或者互相替换文件。</span></span></p>
<p>  <span style="font-size:10.5pt;font-family:Arial;">安装方法：</span></p>
<p><pre>`sudo yum install epel-release</pre></p>
</li>
</ul>

      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://localhost/2014/11/26/CentOS-7安装完第一步：yum源配置/" data-id="cimvrqg1s0001kyfe92vpv59x" class="article-share-link">分享到</a>
      

      
        <a href="http://localhost/2014/11/26/CentOS-7安装完第一步：yum源配置/#ds-thread" class="article-comment-link">评论</a>
      

      
    </footer>
  </div>
  
</article>


  
    <article id="post-CDN，网站加速利器" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2014/11/17/CDN，网站加速利器/" class="article-date">
  <time datetime="2014-11-17T09:04:00.000Z" itemprop="datePublished">2014-11-17</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2014/11/17/CDN，网站加速利器/">CDN，网站加速利器</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <blockquote>
<p>CDN的全称是Content Delivery Network，即内容分发网络。其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节，使内容传输的更快、更稳定。通过在网络各处放置节点服务器所构成的在现有的互联网基础之上的一层智能虚拟网络，CDN系统能够实时地根据网络流量和各节点的连接、负载状况以及到用户的距离和响应时间等综合信息将用户的请求重新导向离用户最近的服务节点上。其目的是使用户可就近取得所需内容，解决 Internet网络拥挤的状况，提高用户访问网站的响应速度。</p>
<p>  –摘自百度百科对CDN的解释</p>
</blockquote>
<p>好吧，一大堆各种术语神码的。其他不说啥，先说说它的作用。当大量用户请求网站数据的时候，并发量比较大，网站服务器对外的端口流量是有限的嘛，会导致用户访问速度下降，降低用户体验。所以呢，为了解决这个问题，人们就把一些网站的静态资源等文件放在多台缓存服务器节点上，然后根据网络流量和各节点的连接、负载状况以及到用户的距离和响应时间等综合信息将用户的请求重新导向离用户最近的服务节点上。为了保证每个缓存服务节点和主站保持数据一致，缓存服务器需要定期访问源站更新缓存，最终目的就是提高网站的访问速度。</p>
<p>1，用了CDN怎么样？有效果嘛？</p>
<p>国内的网站和博客空间很多都用了CDN，比如QQ、Sina、百度，淘宝等，这些网站每天访问量很大，但是依然速度相当地快。SAE、BAE也采用了CDN来提高速度。</p>
<p>2,会不会影响伪静态？</p>
<p>CDN会缓存您网站的静态资源，例如：Javascript、CSS脚本以及图片文件等，不会缓存html文件，所以，只要你不缓存首页，对伪静态是没有影响的。</p>
<p>国内的免费CDN：</p>
<blockquote>
<p><span style="white-space:pre">1、</span><a href="http://yunjiasu.baidu.com/" target="_blank" rel="external">百度云加速</a></p>
<pre><code>&lt;span style=&quot;font-size:12px;&quot;&gt;2、 [加速乐](http://www.jiasule.com/)&lt;/span&gt;

&lt;span style=&quot;font-size:12px;&quot;&gt;3、360云加速     &lt;/span&gt;

&lt;span style=&quot;font-size:12px;&quot;&gt;4、&lt;/span&gt;[安全宝](http://www.anquanbao.com/)&lt;span style=&quot;font-size:12px;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;font-size:12px;white-space: pre;&quot;&gt; &lt;/span&gt;

5、[七牛云存储](https://portal.qiniu.com/)&lt;span style=&quot;font-size:12px;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;font-size:12px;white-space: pre;&quot;&gt; &lt;/span&gt;

6、[又拍云存储](https://www.upyun.com/)

7&lt;span style=&quot;white-space:pre&quot;&gt;、[webluker](http://www.webluker.com/)&lt;/span&gt;
</code></pre></blockquote>

      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://localhost/2014/11/17/CDN，网站加速利器/" data-id="cimvrqg1n0000kyfeaocoq54v" class="article-share-link">分享到</a>
      

      
        <a href="http://localhost/2014/11/17/CDN，网站加速利器/#ds-thread" class="article-comment-link">评论</a>
      

      
    </footer>
  </div>
  
</article>


  
    <article id="post-使用C语言获取linux系统相关信息" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2014/07/24/使用C语言获取linux系统相关信息/" class="article-date">
  <time datetime="2014-07-24T09:54:00.000Z" itemprop="datePublished">2014-07-24</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2014/07/24/使用C语言获取linux系统相关信息/">使用C语言获取linux系统相关信息</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>最近在写shell的时候，涉及到了获取环境变量参数和本地计算机相关信息，包括计算机设备名，用户名的信息，在这里简单总结一下。</p>
<p>获取环境变量各项参数，可以直接使用getenv函数。man中关于getenv函数的声明如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> &amp;lt;stdlib.h&amp;gt;</span></span><br><span class="line"><span class="function"><span class="keyword">char</span> *<span class="title">getenv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *name)</span></span>;</span><br></pre></td></tr></table></figure>
<p>函数中，参数name 为环境变量的名称, 如果该变量存在则会返回指向该内容的指针. 环境变量的格式为name＝value。执行成功则返回指向该内容的指针, 找不到符合的环境变量名称则返回NULL。</p>
<p>这里我使用环境变量获取用户名称和当前目录为例，首先我们知道在环境变量中，用户名保存在“LOGNAME”中，而当前目录保存在&quot;PWD&quot;中，所以函数调用应该为</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">getenv(“LOGNAME”)</span><br><span class="line">getenv(“PWD”);</span><br></pre></td></tr></table></figure>
<p>所以，简单的测试程序就出来了，程序源码如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc,<span class="keyword">char</span> **argv)</span></span><br><span class="line"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> *name;</span><br><span class="line">    <span class="keyword">char</span> *path;</span><br><span class="line"></span><br><span class="line">    name = getenv(<span class="string">"LOGNAME"</span>);</span><br><span class="line">    path = getenv(<span class="string">"PWD"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"logname = %s\n"</span>,name);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"pwd = %s\n"</span>,path);</span><br><span class="line"></span><br><span class="line">    return <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>测试程序没有包含错误处理，如有需要，请自行加上。在我的电脑上运行如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">wangbo@iDina-PC:~/linuxc/tmp$ ./a.out</span><br><span class="line">logname = wangbo</span><br><span class="line">pwd = /home/wangbo/linuxc/tmp</span><br></pre></td></tr></table></figure>
<p>但是呢，我们试着请通过环境变量获取系统设备名称，是无法获取的。原因我也不太清楚，如果知道的话，赶快告诉我吧。</p>
<p>不能从环境变量获取设备名，就只能从其他地方获取了。恩，这里给大家介绍一个函数，gethostname函数，可以直接获取系统设备名。</p>
<p>声明如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">gethostname</span><span class="params">(<span class="keyword">char</span> *name, size_t len)</span></span>;</span><br></pre></td></tr></table></figure>
<p>函数中，name是指向你所设置的保存设备名的字符串的指针，而len是name的大小，成功的话返回0,获取失败，返回-1,错误代码保存在errno中。</p>
<p>函数比较简单，这里直接给出测试代码：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> **argv)</span></span><br><span class="line"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> hostname[<span class="number">32</span>];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>( gethostname(hostname,sizeof(hostname)) )</span><br><span class="line">    &#123;</span><br><span class="line">        perror(<span class="string">"gethostname"</span>);</span><br><span class="line">        return (<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"localhost name:%s\n"</span>,hostname);</span><br><span class="line"></span><br><span class="line">    return <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在我的电脑上运行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">wangbo@iDina-PC:~/linuxc/tmp$ ./a.out</span><br><span class="line">localhost name:iDina-PC</span><br></pre></td></tr></table></figure>
<p>后来查询相关资料，又发现一个函数，uname，可以获取系统包括版本在内的相关信息，函数声明如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/utsname.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">uname</span><span class="params">(<span class="keyword">struct</span> utsname *buf)</span></span>;</span><br></pre></td></tr></table></figure>
<p>struct utsname 是一个系统定义好的结构体，结构体定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> utsname&#123;</span><br><span class="line">    <span class="keyword">char</span> sysname[];       <span class="comment">/* 操作系统名称 (e.g., "Linux") */</span></span><br><span class="line">    <span class="keyword">char</span> nodename[];      <span class="comment">/* 当前设备在网络上的名称 (e.g,"localhost.localdomain")*/</span></span><br><span class="line">    <span class="keyword">char</span> release[];       <span class="comment">/* 系统发布级别(e.g., "2.6.28") */</span></span><br><span class="line">    <span class="keyword">char</span> version[];       <span class="comment">/* 系统发布版本 */</span></span><br><span class="line">    <span class="keyword">char</span> machine[];       <span class="comment">/* 当前设备硬件体系(e.g.,"i686") */</span></span><br><span class="line">    <span class="meta">#<span class="meta-keyword">ifdef</span> _GNU_SOURCE</span></span><br><span class="line">    <span class="keyword">char</span> domainname[];    <span class="comment">/* NIS或者YP域名 */</span></span><br><span class="line">    <span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>函数正确返回0,错误返回-1.错误代码保存在errno中。</p>
<p>和上面一样，比较简单，测试代码如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/utsname.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc,<span class="keyword">char</span> **argv)</span></span><br><span class="line"></span>&#123;</span><br><span class="line">    <span class="keyword">struct</span> utsname buf;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(uname(&amp;buf))</span><br><span class="line">    &#123;</span><br><span class="line">        perror(<span class="string">"uname"</span>);</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"sysname:%s\n"</span>,buf.sysname);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"nodename:%s\n"</span>,buf.nodename);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"release:%s\n"</span>,buf.release);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"version:%s\n"</span>,buf.version);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"machine:%s\n"</span>,buf.machine);</span><br><span class="line"></span><br><span class="line">    return <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我的电脑上运行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">wangbo@iDina-PC:~/linuxc/tmp$ ./a.out </span><br><span class="line">sysname:Linux</span><br><span class="line">nodename:iDina-PC</span><br><span class="line">release:3.13.0-32-generic</span><br><span class="line">version:#57-Ubuntu SMP Tue Jul 15 03:51:12 UTC 2014</span><br><span class="line">machine:i686</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://localhost/2014/07/24/使用C语言获取linux系统相关信息/" data-id="cimvrqg1z0005kyfe0ghzbpfk" class="article-share-link">分享到</a>
      

      
        <a href="http://localhost/2014/07/24/使用C语言获取linux系统相关信息/#ds-thread" class="article-comment-link">评论</a>
      

      
    </footer>
  </div>
  
</article>


  
    <article id="post-如何使用getopt-函数解析参数" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2014/07/18/如何使用getopt-函数解析参数/" class="article-date">
  <time datetime="2014-07-18T01:28:39.000Z" itemprop="datePublished">2014-07-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2014/07/18/如何使用getopt-函数解析参数/">如何使用getopt()函数解析参数</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>最近在写程序的过程中，把一部分时间都花费在程序对参数的处理上。今天听了学长说到getopt函数，才发现原来c里面还有一个专门解决参数处理的函数，查询了相关资料，这里简单总结一下。</p>
<p>使用<code>int main( int argc, char *argv[] )</code>或<code>int main( int argc, char **argv )</code>时，系统将把用户输入的参数通过argc和argv引入程序中，argc为参数的个数，argv是指向参数的指针数组，其中第一个参数为自身程序文件名。</p>
<p>这里我们使用getopt() 函数对传入的参数进行处理，getopt()位于 unistd.h 系统头文件中，函数原型可以在man中查到，大家可以使用下面指令查看getopt的man手册<code>man 3 getopt</code></p>
<p>getopt的函数原型如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;unistd.h&gt;</span><br><span class="line">int getopt(int argc, char * const argv[], const char *optstring);</span><br><span class="line"></span><br><span class="line">extern char *optarg;</span><br><span class="line">extern int optind, opterr, optopt;</span><br></pre></td></tr></table></figure></p>
<p>这里引用IBM对于其的解释，<a href="http://www.ibm.com/developerworks/cn/aix/library/au-unix-getopt.html" title="使用 getopt()进行命令行处理" target="_blank" rel="external">原文链接在这</a>。我对其中部分错误做了更改，</p>
<blockquote>
<p>给定了命令参数的数量 (argc)、指向这些参数的数组 (argv) 和选项字符串 (optstring) 后，getopt() 将返回第一个选项，并设置一些全局变量。使用相同的参数再次调用该函数时，它将返回下一个选项，并设置相应的全局变量。如果不再有识别到的选项，将返回 -1，此任务就完成了。</p>
<p>getopt() 所设置的全局变量包括：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&gt; <span class="keyword">extern</span> <span class="keyword">char</span> *optarg;  <span class="comment">//选项的参数指针</span></span><br><span class="line">&gt; <span class="keyword">extern</span> <span class="keyword">int</span> optind,  <span class="comment">//下一次调用getopt的时，从optind存储的位置处重新开始检查选项。</span></span><br><span class="line">&gt; <span class="keyword">extern</span> <span class="keyword">int</span> opterr,  <span class="comment">//当opterr=0时，getopt不向stderr输出错误信息。</span></span><br><span class="line">&gt; <span class="keyword">extern</span> <span class="keyword">int</span> optopt;  <span class="comment">//当命令行选项字符不包括在optstring中或者选项缺少必要的参数时，该选项存储在optopt中，getopt返回'?'</span></span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
<p>对于每个选项，选项字符串 (optstring) 中都包含一个对应的字符。具有参数的选项后面跟有一个 : 字符。如果一个选项后面紧跟2个冒号（::），表示该选项和参数之间不使用空格隔开；</p>
<p>可以重复调用 getopt()，直到其返回 -1 为止；任何剩下的命令行参数通常视为文件名或程序相应的其他内容。</p>
</blockquote>
<p>我们知道，一般程序的选项分为两种：带关联值的和不带关联值的，</p>
<p>在解析参数的时候，我们希望的是：从一堆参数中把我们要的选项和选项关联值分别找到并存到相对应的地方供我们使用，并且把其他参数找出来供我们调用。</p>
<p>getopt完美实现这一点。</p>
<p>我们在测试的时候，定义了一个全局变量来存储参数值，<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="keyword">struct</span> parameter</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span>     a;        <span class="comment">//参数a</span></span><br><span class="line">    <span class="keyword">int</span>     b;        <span class="comment">//参数b</span></span><br><span class="line">    <span class="keyword">char</span>    *b_pri;   <span class="comment">//参数b的关联值</span></span><br><span class="line">    <span class="keyword">int</span>     c;        <span class="comment">//参数c</span></span><br><span class="line">    <span class="keyword">char</span>    *c_pri;   <span class="comment">//参数c的关联值</span></span><br><span class="line">&#125; par;</span><br></pre></td></tr></table></figure></p>
<p>如果遇到了其他选项，getopt() 将显示一个错误消息，程序将在显示了使用方法消息后退出。因此optString应该为<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">char</span> *optstring = <span class="string">"ab:c::?"</span>;</span><br></pre></td></tr></table></figure></p>
<p>程序运行之前，我们先对参数初始化，设定其初始值。<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//参数初始化</span></span><br><span class="line">opt.a = <span class="number">0</span>;</span><br><span class="line">opt.b = <span class="number">0</span>;</span><br><span class="line">opt.b_pri = <span class="literal">NULL</span>;</span><br><span class="line">opt.c = <span class="number">0</span>;</span><br><span class="line">opt.c_pri = <span class="literal">NULL</span>;</span><br></pre></td></tr></table></figure></p>
<p>接下来就简单的把参数使用getopt处理，最后输出最后的参数就行了，看看我们的测试程序。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">struct</span> parameter</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span>       a;        <span class="comment">//参数a</span></span><br><span class="line">    <span class="keyword">int</span>       b;        <span class="comment">//参数b</span></span><br><span class="line">    <span class="keyword">char</span> *b_pri;        <span class="comment">//参数b的关联值</span></span><br><span class="line">    <span class="keyword">int</span>       c;        <span class="comment">//参数c</span></span><br><span class="line">    <span class="keyword">char</span> *c_pri;        <span class="comment">//参数c的关联值</span></span><br><span class="line">&#125;par;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">( <span class="keyword">int</span> argc, <span class="keyword">char</span> **argv )</span></span><br><span class="line"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i;        <span class="comment">//循环变量</span></span><br><span class="line">    par opt;    <span class="comment">//参数</span></span><br><span class="line">    <span class="keyword">int</span> flag = <span class="number">0</span>;    <span class="comment">//循环退出标志位</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//参数初始化</span></span><br><span class="line">    opt.a = <span class="number">0</span>;</span><br><span class="line">    opt.b = <span class="number">0</span>;</span><br><span class="line">    opt.b_pri = <span class="literal">NULL</span>;</span><br><span class="line">    opt.c = <span class="number">0</span>;</span><br><span class="line">    opt.c_pri = <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//参数检测</span></span><br><span class="line">    <span class="keyword">if</span> (argc == <span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"您没有设置选项！\n"</span>);</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//输出未处理的参数</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"系统传入的参数为:"</span>);</span><br><span class="line">    <span class="keyword">for</span> ( i = <span class="number">1</span>; i &lt; argc; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%s "</span>,argv[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//循环处理传入参数</span></span><br><span class="line">    <span class="keyword">while</span>(flag != <span class="number">-1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//调用getopt处理参数</span></span><br><span class="line">        <span class="keyword">switch</span>(getopt( argc, argv, <span class="string">"ab:c::?"</span>))</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'a'</span>:</span><br><span class="line">                opt.a = <span class="number">1</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'b'</span>:</span><br><span class="line">                opt.b = <span class="number">1</span>;</span><br><span class="line">                opt.b_pri = optarg;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'c'</span>:</span><br><span class="line">                opt.c = <span class="number">1</span>;</span><br><span class="line">                opt.c_pri = optarg;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">-1</span>:</span><br><span class="line">                flag = <span class="number">-1</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>( optind != argc)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"参数中非程序选项的有:"</span>);</span><br><span class="line">        <span class="keyword">for</span> (i = optind; i &lt; argc; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">"%s\t"</span>,argv[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//输出解析结果</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"解析到程序启动后启用的选项有："</span>);</span><br><span class="line">    <span class="keyword">if</span> (opt.a == <span class="number">1</span>)</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"a,"</span>);</span><br><span class="line">    <span class="keyword">if</span> (opt.b == <span class="number">1</span>)</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"b(参数为:%s),"</span>,opt.b_pri);</span><br><span class="line">    <span class="keyword">if</span> (opt.c == <span class="number">1</span>)</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"c(参数为:%s),"</span>,opt.c_pri);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//处理后，输出全部参数与原来对比</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"使用getopt后，系统参数变为为:"</span>);</span><br><span class="line">    <span class="keyword">for</span> ( i = <span class="number">1</span>; i &lt; argc; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%s "</span>,argv[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line">    return <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>保存后，编译运行。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ ./a.out -a -b 123 3 22 -h -c1</span><br><span class="line">系统传入的参数为:-a -b 123 3 22 -h -c1</span><br><span class="line">./a.out: invalid option -- &apos;h&apos;</span><br><span class="line">参数中非程序选项的有:3    22    </span><br><span class="line">解析到程序启动后启用的选项有：a,b(参数为:123),c(参数为:1),</span><br><span class="line">使用getopt后，系统参数变为为:-a -b 123 -h -c1 3 22</span><br></pre></td></tr></table></figure></p>
<p>其中，我们传入的参数被全部解析出来，非正常选项”-h”直接显示出来，如果大家不希望非正常参数显示出来，或者希望自己处理非正常参数，可以在设置getopt中全域变量opterr = 0，在optstring的时候不加入？，例如<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">opterr = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">char</span> *optstring = <span class="string">"ab:c::"</span>;</span><br></pre></td></tr></table></figure></p>
<p>那么程序在检测到未知选项会返回？，我们可以这样处理</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">opterr = <span class="number">0</span>;    <span class="comment">//getopt不输出错误参数</span></span><br><span class="line"></span><br><span class="line">...其他程序段...</span><br><span class="line"></span><br><span class="line"><span class="comment">//循环处理传入参数</span></span><br><span class="line"><span class="keyword">while</span>(flag != <span class="number">-1</span>)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//调用getopt处理参数</span></span><br><span class="line">    <span class="keyword">switch</span>(getopt( argc, argv, <span class="string">"ab:c::"</span>))</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'a'</span>:</span><br><span class="line">            opt.a = <span class="number">1</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'b'</span>:</span><br><span class="line">            opt.b = <span class="number">1</span>;</span><br><span class="line">            opt.b_pri = optarg;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'c'</span>:</span><br><span class="line">            opt.c = <span class="number">1</span>;</span><br><span class="line">            opt.c_pri = optarg;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'?'</span>:</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">"出现非正常选项：%c\n"</span>,optopt);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">-1</span>:</span><br><span class="line">            flag = <span class="number">-1</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>程序源代码如下：<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">struct</span> parameter</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span>       a;        <span class="comment">//参数a</span></span><br><span class="line">    <span class="keyword">int</span>       b;        <span class="comment">//参数b</span></span><br><span class="line">    <span class="keyword">char</span> *b_pri;    <span class="comment">//参数b的关联值</span></span><br><span class="line">    <span class="keyword">int</span>       c;        <span class="comment">//参数c</span></span><br><span class="line">    <span class="keyword">char</span> *c_pri;    <span class="comment">//参数c的关联值</span></span><br><span class="line">&#125;par;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">( <span class="keyword">int</span> argc, <span class="keyword">char</span> **argv )</span></span><br><span class="line"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i;        <span class="comment">//循环变量</span></span><br><span class="line">    par opt;    <span class="comment">//参数</span></span><br><span class="line">    <span class="keyword">int</span> flag = <span class="number">0</span>;    <span class="comment">//循环退出标志位</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//参数初始化</span></span><br><span class="line">    opt.a = <span class="number">0</span>;</span><br><span class="line">    opt.b = <span class="number">0</span>;</span><br><span class="line">    opt.b_pri = <span class="literal">NULL</span>;</span><br><span class="line">    opt.c = <span class="number">0</span>;</span><br><span class="line">    opt.c_pri = <span class="literal">NULL</span>;</span><br><span class="line">    opterr = <span class="number">0</span>;    <span class="comment">//getopt不输出错误参数</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//参数检测</span></span><br><span class="line">    <span class="keyword">if</span> (argc == <span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"您没有设置选项！\n"</span>);</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//输出未处理的参数</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"系统传入的参数为:"</span>);</span><br><span class="line">    <span class="keyword">for</span> ( i = <span class="number">1</span>; i &lt; argc; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%s "</span>,argv[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//循环处理传入参数</span></span><br><span class="line">    <span class="keyword">while</span>(flag != <span class="number">-1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//调用getopt处理参数</span></span><br><span class="line">        <span class="keyword">switch</span>(getopt( argc, argv, <span class="string">"ab:c::"</span>))</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'a'</span>:</span><br><span class="line">                opt.a = <span class="number">1</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'b'</span>:</span><br><span class="line">                opt.b = <span class="number">1</span>;</span><br><span class="line">                opt.b_pri = optarg;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'c'</span>:</span><br><span class="line">                opt.c = <span class="number">1</span>;</span><br><span class="line">                opt.c_pri = optarg;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'?'</span>:</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">"出现非正常选项：%c\n"</span>,optopt);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">-1</span>:</span><br><span class="line">                flag = <span class="number">-1</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>( optind != argc)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"参数中非程序选项的有:"</span>);</span><br><span class="line">        <span class="keyword">for</span> (i = optind; i &lt; argc; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">"%s\t"</span>,argv[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//输出解析结果</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"解析到程序启动后启用的选项有："</span>);</span><br><span class="line">    <span class="keyword">if</span> (opt.a == <span class="number">1</span>)</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"a,"</span>);</span><br><span class="line">    <span class="keyword">if</span> (opt.b == <span class="number">1</span>)</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"b(参数为:%s),"</span>,opt.b_pri);</span><br><span class="line">    <span class="keyword">if</span> (opt.c == <span class="number">1</span>)</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"c(参数为:%s),"</span>,opt.c_pri);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//处理后，输出全部参数与原来对比</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"使用getopt后，系统参数变为为:"</span>);</span><br><span class="line">    <span class="keyword">for</span> ( i = <span class="number">1</span>; i &lt; argc; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%s "</span>,argv[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line">    return <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>这样的话，上面同样的参数下，运行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ ./a.out -a -b 123 3 22 -h -c1</span><br><span class="line">系统传入的参数为:-a -b 123 3 22 -h -c1</span><br><span class="line">出现非正常选项：h</span><br><span class="line">参数中非程序选项的有:3    22    </span><br><span class="line">解析到程序启动后启用的选项有：a,b(参数为:123),c(参数为:1),</span><br><span class="line">使用getopt后，系统参数变为为:-a -b 123 -h -c1 3 22</span><br></pre></td></tr></table></figure>
<p>ok了，其实还有一个比getopt更好的一个选择，就是getopt_long，它可以支持长选项，因为这篇博文呢针对getopt的，所以我就不多做介绍，大家可以查man手册，或者等待我不知道什么时候的下次更新吧。</p>

      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://localhost/2014/07/18/如何使用getopt-函数解析参数/" data-id="cimvrqg220006kyfe7lj5y1oa" class="article-share-link">分享到</a>
      

      
        <a href="http://localhost/2014/07/18/如何使用getopt-函数解析参数/#ds-thread" class="article-comment-link">评论</a>
      

      
    </footer>
  </div>
  
</article>


  
    <article id="post-linux下挂载NTFS分区错误修复" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2014/06/15/linux下挂载NTFS分区错误修复/" class="article-date">
  <time datetime="2014-06-15T13:49:25.000Z" itemprop="datePublished">2014-06-15</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2014/06/15/linux下挂载NTFS分区错误修复/">linux下挂载NTFS分区错误修复</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>今天在linux下打开win的NTFS硬盘总是提示出错了，而且是全部的NTFS盘都出错，其中sda1错误显示如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Error mounting /dev/sda1 at /media/wangbo/24F02EECF02EC3C0: Command-line `mount -t &quot;ntfs&quot; -o &quot;uhelper=udisks2,nodev,nosuid,uid=1000,gid=1000,dmask=0077,fmask=0177&quot; &quot;/dev/sda1&quot; &quot;/media/wangbo/24F02EECF02EC3C0&quot;&apos; exited with non-zero exit status 14: Windows is hibernated, refused to mount.</span><br><span class="line">Failed to mount &apos;/dev/sda1&apos;: Operation not permitted</span><br><span class="line">The NTFS partition is in an unsafe state. Please resume and shutdown</span><br><span class="line">Windows fully (no hibernation or fast restarting), or mount the volume</span><br><span class="line">read-only with the &apos;ro&apos; mount option.</span><br></pre></td></tr></table></figure>
<p>恩，挂载出错，好像是因为win8的状态是高级休眠，试着使用ntfsfix修复一下，<br>如果没有安装得先安装,执行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install ntfs-3g</span><br></pre></td></tr></table></figure>
<p>因为我已经安装过了，所以。。。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">[sudo] password for wangbo:</span><br><span class="line">Reading package lists... Done</span><br><span class="line">Building dependency tree       </span><br><span class="line">Reading state information... Done</span><br><span class="line">ntfs-3g is already the newest version.</span><br><span class="line">The following packages were automatically installed and are no longer required:</span><br><span class="line">  gir1.2-appindicator3-0.1 gir1.2-javascriptcoregtk-3.0 gir1.2-webkit-3.0 gir1.2-xkl-1.0 python3-cairo</span><br><span class="line">  python3-crypto python3-gi-cairo python3-icu python3-keyring python3-pyicu python3-secretstorage</span><br><span class="line">Use &apos;apt-get autoremove&apos; to remove them.</span><br><span class="line">0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.</span><br></pre></td></tr></table></figure>
<p>安装好后，对原来挂载出错的NTFS盘分别执行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo ntfsfix /dev/sda1</span><br><span class="line">sudo ntfsfix /dev/sda8</span><br></pre></td></tr></table></figure>
<p>修复<span style="font-family: SimSun;">分区错误，结果呢，</span></p>
<p>修复sda8的时候，成功修复。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">wangbo@Idina:~/temp$ sudo ntfsfix /dev/sda8</span><br><span class="line">Mounting volume... The disk contains an unclean file system (0, 0).</span><br><span class="line">Metadata kept in Windows cache, refused to mount.</span><br><span class="line">FAILED</span><br><span class="line">Attempting to correct errors...</span><br><span class="line">Processing $MFT and $MFTMirr...</span><br><span class="line">Reading $MFT... OK</span><br><span class="line">Reading $MFTMirr... OK</span><br><span class="line">Comparing $MFTMirr to $MFT... OK</span><br><span class="line">Processing of $MFT and $MFTMirr completed successfully.</span><br><span class="line">Setting required flags on partition... OK</span><br><span class="line">Going to empty the journal ($LogFile)... OK</span><br><span class="line">Checking the alternate boot sector... OK</span><br><span class="line">NTFS volume version is 3.1.</span><br><span class="line">NTFS partition /dev/sda8 was processed successfully.</span><br></pre></td></tr></table></figure>
<p>但是在修复sda1的时候，因为win8高级休眠，修复失败了。。。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">wangbo@Idina:~/temp$ sudo ntfsfix /dev/sda1</span><br><span class="line">Mounting volume... Windows is hibernated, refused to mount.</span><br><span class="line">FAILED</span><br><span class="line">Attempting to correct errors...</span><br><span class="line">Processing $MFT and $MFTMirr...</span><br><span class="line">Reading $MFT... OK</span><br><span class="line">Reading $MFTMirr... OK</span><br><span class="line">Comparing $MFTMirr to $MFT... OK</span><br><span class="line">Processing of $MFT and $MFTMirr completed successfully.</span><br><span class="line">Setting required flags on partition... OK</span><br><span class="line">Going to empty the journal ($LogFile)... OK</span><br><span class="line">Windows is hibernated, refused to mount.</span><br><span class="line">Remount failed: Operation not permitted</span><br></pre></td></tr></table></figure>
<p>恩，解决办法，进win8，关闭快速启动，恩，对，关了它就ok</p>
<p>关闭办法是控制面板，在电源管理中，选择关闭盖子的功能，更改不能更改的选项，去掉快速启动的钩，nice，好了，重启进linux，ok</p>

      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://localhost/2014/06/15/linux下挂载NTFS分区错误修复/" data-id="cimvrqg1y0004kyfetetmhdl5" class="article-share-link">分享到</a>
      

      
        <a href="http://localhost/2014/06/15/linux下挂载NTFS分区错误修复/#ds-thread" class="article-comment-link">评论</a>
      

      
    </footer>
  </div>
  
</article>


  
  
</section>
      
      <aside id="sidebar">
  
    
  
    
  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/">五月 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/04/">四月 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/12/">十二月 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/11/">十一月 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/07/">七月 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/06/">六月 2014</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">近期文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2015/05/01/Linux服务器配置与LNMP安装实录-1/">Linux服务器配置与LNMP安装实录---(1)</a>
          </li>
        
          <li>
            <a href="/2015/04/16/科学上网，简易教程/">科学上网，简易教程</a>
          </li>
        
          <li>
            <a href="/2014/12/17/记Fedora-21-Yun源配置和软件安装/">记Fedora 21 Yun源配置和软件安装</a>
          </li>
        
          <li>
            <a href="/2014/12/09/Linux运维文档之nginx【转】/">Linux运维文档之nginx【转】</a>
          </li>
        
          <li>
            <a href="/2014/11/26/CentOS-7安装完第一步：yum源配置/">CentOS 7安装完第一步：yum源配置</a>
          </li>
        
      </ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">友情链接</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="http://xiguabaobao.com" target="_blank">主题作者</a>
          </li>
        
          <li>
            <a href="http://reqianduan.com" target="_blank">热前端</a>
          </li>
        
          <li>
            <a href="https://www.xiyoulinux.org" target="_blank">西邮linux兴趣小组</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
      
    </div>
    <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2016 wangbo<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
      .
      Theme by <a href="https://github.com/xiangming/landscape-plus" target="_blank">Landscape-plus</a>
    </div>
  </div>
</footer>
  </div>
  <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
  <!-- totop start -->
<div id="totop">
<a title="返回顶部"><img src="/img/scrollup.png"/></a>
</div>

<!-- totop end -->

<!-- 多说公共js代码 start -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"idina"};
  (function() {
    var ds = document.createElement('script');
    ds.type = 'text/javascript';ds.async = true;
    ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
    ds.charset = 'UTF-8';
    (document.getElementsByTagName('head')[0]
     || document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
  </script>
<!-- 多说公共js代码 end -->


<!-- 百度分享 start -->

<!-- 百度分享 end -->

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>




<script src="/js/script.js"></script>

</div>
</body>
</html>
